﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace TCP_Server
{
    internal class Socket_TCP_Server
    {
        public static Form1 form1obj = new Form1();

        public static string Server_IP = null;
        public static string Serrver_Port = null;

        public static string Client_IP = null;
        public static string Client_Port = null;

        public static Socket Server_so = null; // 服务器
        public static Socket Client_so = null; // 记录客户端

        public static Dictionary<string, Socket> mySocketDict = new Dictionary<string, Socket>(); // 字典

        public static void server_start()
        {
            Server_so = new Socket(SocketType.Stream, ProtocolType.Tcp); // 实例化
            EndPoint localEP = new IPEndPoint(IPAddress.Parse(Server_IP), int.Parse(Serrver_Port)); // 实例化
            Server_so.Bind(localEP);
            Server_so.Listen(2);
        }

        public static void server_close()
        {
            if (Server_so != null)
            {
                Server_so.Close(100);
                Server_so = null;
            }
            if (Client_so != null)
            {
                Client_so.Close(100);
                Client_so = null;
            }
        }

        public static void Server_send(Socket soc, string str) // 发送
        {
            byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
            soc.Send(buffer);
        }

        public static string Receive(Socket Client_so) // 接收
        {
            byte[] Receive_Data = new byte[500];
            int len = 0;
            string str = "";
            len = Client_so.Receive(Receive_Data);
            if (len < 1)
            {
                return false.ToString();
            }
            str = System.Text.Encoding.Default.GetString(Receive_Data, 0, len) + "\r\n";
            return str;
        }

        public static void judge_puls(string data, out string ip, out string port)
        {
            string data_head = "[::ffff:";
            string data_tail = "]:";

            //int start = data.IndexOf(data_head);
            //int end = data.IndexOf(data_tail);

            //if (start != -1 && end != -1 && start < end)
            //{
            //    int data_start = data.IndexOf(data_head);
            //    int data_end = data.IndexOf(data_tail);
            //    int data_size = data_head.Length;
            //    if (data_start != -1 && data_end != -1 && data_start + data_size < data_end)
            //    {
            //        ip = data.Substring(data_start + data_size, data_end - (data_start + data_size));
            //        port = data.Substring(data_end + data_tail.Length);
            //    }
            //}

            int data_start = data.IndexOf(data_head);
            int data_end = data.IndexOf(data_tail);
            int data_size = data_head.Length;

            ip = data.Substring(data_start + data_size, data_end - (data_start + data_size));
            port = data.Substring(data_end + data_tail.Length);
        }

        public static void add_ip_port(string str)
        {
            form1obj.comboBox_客户端列表.Items.Add(str);
        }
    }
}
